$(document).ready(function () {
    init();
});

function init() {
    var channel = art.dialog.data('channel');
    var phoneType = art.dialog.data('phoneType');

    $.ajax({
        url: APP_ADDRESS + "/app/version/history?channel=" + channel + "&phoneType=" + phoneType,
        type: "GET",
        contentType: 'application/json',
        cache: false,
        success: function (data) {
            if (data.resultCode == '200') {
                if (data.result != null && data.result.length > 0) {
                    frontEndPager.init(data.result.length, list);

                    list();
                }
            }
        },
        error: function (data) {
            console.log("查询失败: " + data.result);
            console.log(data);
        }
    });
}

function list() {
    var channel = art.dialog.data('channel');
    var phoneType = art.dialog.data('phoneType');

    $.ajax({
        url: APP_ADDRESS + "/app/version/history?channel=" + channel + "&phoneType=" + phoneType,
        type: "GET",
        contentType: 'application/json',
        cache: false,
        success: function (data) {
            if (data.resultCode == '200') {
                $('#tableList tbody').empty();

                var tr = '';
                if (data.result != null && data.result.length > 0) {
                    var index = (frontEndPager.getCurrentPage() - 1) * frontEndPager.pageSize;
                    var end = (frontEndPager.getCurrentPage() - 1) * frontEndPager.pageSize + frontEndPager.pageSize;
                    if (end > data.result.length)
                        end = data.result.length;

                    console.log(index + " -> " + end);

                    for (var i = index; i < end; i++) {
                        var singleObj = data.result[i];

                        tr += '<tr><td>'
                            + '<input type="hidden" id ="id" name="id" value="' + singleObj.id + '"/>'
                            + (i + 1)
                            + '</td><td>'
                            + singleObj.channelText  //客户端
                            + '</td><td>'
                            + singleObj.version   //版本号
                            + '</td><td>'
                            + singleObj.phoneType //操作系统
                            + '</td><td>'
                            + singleObj.isFocusUpdateText //强制更新
                            + '</td><td>'
                            + singleObj.name //标题
                            + '</td><td>'
                            + singleObj.description //描述
                            + '</td><td>'
                            + singleObj.LastUpdated //时间
                            + '</td><td>'
                            + singleObj.operator //操作人
                            + '</td></tr>';
                    }

                }
                else {
                    tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
                }

                $('#tableList tbody').html(tr);

            }
        },
        error: function (data) {
            console.log("查询失败: " + data.result);
            console.log(data);
        }
    });
}
